package com.inspinia.upms.rpc.service;

import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.base.util.CollectionUtil;
import com.inspinia.upms.common.model.dept.UpmsDept;
import com.inspinia.upms.common.model.org.UpmsOrganization;
import com.inspinia.upms.common.model.role.UpmsRole;
import com.inspinia.upms.dao.UpmsRoleDao;
import com.inspinia.upms.rpc.api.UpmsDeptService;
import com.inspinia.upms.rpc.api.UpmsOrganizationService;
import com.inspinia.upms.rpc.api.UpmsRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.ValidationException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 角色 业务层处理
 *
 * @author ruoyi
 */
@Service
public class UpmsRoleServiceImpl extends BaseServiceImpl<UpmsRole> implements UpmsRoleService {

    @Resource
    private UpmsRoleDao roleDao;
    @Resource
    UpmsDeptService departmentService;
    @Resource
    UpmsOrganizationService organizationService;
    @Override
    protected BaseDao<UpmsRole> getDao() {
        return roleDao;
    }

    @Override
    @Transactional
    public int insert(UpmsRole bean) {
        validate(bean);
        return super.insert(bean);
    }

    @Override
    @Transactional
    public int update(UpmsRole bean) {
        validate(bean);
        return super.update(bean);
    }

    /**
     * 校验角色信息
     *
     * @param bean
     */
    private void validate(UpmsRole bean) {
        boolean b = validateName(bean.getRoleName(), bean.getId());
        if (b) {
            throw new ValidationException("角色名已存在");
        }
    }

    @Override
    public boolean validateName(String roleName, Long roleId) {
        return roleDao.countByName(roleName, roleId) > 0;
    }

    @Override
    public List<Long> getResources(Long roleId) {
        return roleDao.findResourceIdByRoleId(roleId);
    }

    @Override
    public int setResources(Long[] resourceIds, Long roleId) {
        if (resourceIds == null) {
            return roleDao.deleteResources(roleId);
        } else {
            //先删除角色的资源，然后再添加新的资源。
            roleDao.deleteResources(roleId);
            int i = 0;
            for (Long resourceId :  resourceIds){
                i = roleDao.addResources(roleId, resourceId);
            }
            return i;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long roleId){
        //删除角色资源关联表
        roleDao.deleteResources(roleId);
        //删除用户角色
        roleDao.deleteUserRole(roleId);
        //删除角色
        return roleDao.delete(roleId);
    }

//    @Override
//    public Pager<Role> findPage(int pageNo, int pageSize) {
//        return roleDao.findPage(pageNo,pageSize);
//    }

    @Override
    public List<UpmsRole> findByIds(List<Long> roleIds) {
        return roleDao.findByIds(roleIds);
    }

    @Override
    public List<UpmsRole> findAll(){
        return roleDao.findAll();
    }

    @Override
    public List<UpmsRole> findAll(List<Long> depts) {
        //需要配置的角色参数
        String roleData = "";
        List<UpmsRole> roleList = roleDao.findAll();
        if(CollectionUtil.isNotEmpty(depts)){
            Long deptId = depts.get(depts.size()-1);
            UpmsDept department = departmentService.get(deptId);
            UpmsOrganization organization = null;
            if(department!=null){
                organization = organizationService.get(department.getOrgId());
            }
            Iterator<UpmsRole> stringIterator = roleList.iterator();
            while (stringIterator.hasNext()){
                UpmsRole role = stringIterator.next();
                if(!roleData.contains(role.getRoleName())){
                    stringIterator.remove();
                }
            }
        }
        return roleList;
    }

    @Override
    public List<String> findRoleNamesByUserId(Long userId) {

        return roleDao.findRoleNamesByUserId(userId);
    }

    @Override
    public Long getIdByRoleName(String roleName) {
        return roleDao.getIdByRoleName(roleName);
    }


    @Override
    public List<Long> findOutUserIsRole(List<Long> userIds, Long roleId) {
        if (CollectionUtil.isNotEmpty(userIds) && roleId != null) {
            return roleDao.findOutUserIsRole(userIds, roleId);
        }
        return new ArrayList<>(0);
    }

    @Override
    public List<Long> findUserIdsByRoleId(Long roleId) {
        return roleDao.findUserIdsByRoleId(roleId);
    }

    @Override
    public List<UpmsRole> findRoleByUserId(Long userId) {
        return roleDao.getRoleByUserId(userId);
    }
}
